<?php


namespace app\admin\model\User;


use app\mall\model\Merchant;
use think\facade\Db;
use think\Model;

class UserModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "user";

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserWhereMore($where)
    {
        $list = Db::name("user")->where($where)->select()->toArray();
        return $list;
    }

    public static function getUserWhereMores($where, $field = '*')
    {
        $list = Db::name("user")->where($where)->field($field)->select()->toArray();
        return $list;
    }

    /**
     * @param $value
     * @return mixed
     * 身份标识
     */
    public function getIdentityAttr($value)
    {
        $identity = [0 => '普通', 1 => '老师'];
        return $identity[$value];
    }

    /**
     * @return \think\model\relation\HasOne
     * 小店
     */
    public function merchant()
    {
        return $this->hasOne(Merchant::class, 'id', 'user_id');
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getUserCountWhere($where)
    {
        $count = Db::name("user")->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserListWhere($page, $limit, $where = [], $field = "*")
    {
        $start = ceil(($page - 1) * $limit);
        $list = self::field($field)->where($where)->limit($start, $limit)->select()->toArray();
        return $list;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addUser($data)
    {
        $id = Db::name('user')->insertGetId($data);
        return $id;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateUserWhere($where, $save)
    {
        $flag = Db::name('user')->where($where)->update($save);
        return $flag;
    }

    /**
     * 删除
     * @param $where
     * @return int
     * @throws
     */
    public static function delUserWhere($where)
    {
        $flag = Db::name('user')->where($where)->delete();
        return $flag;
    }

}
